#pragma once
#include "uart/ProtocolSender.h"
#include "uiplayer/ImageAnimView.h"
#include "can/context.h"
#include "logic_utility.hpp"
#include "uiplayer/WindowManager.hpp"


V_Condition info0;
Vehicle_info1 info1;
Vehicle_info3 info3;
int speedStart = 0;
int speedEnd = 0;
int tachStart = 0;
int tachEnd = 0;

namespace{
	CanDataCb _s_can_cb;
	// 窗口列表
	std::vector<ZKWindow*> globalWindows;
	// 创建窗口管理器
	WindowManager windowManager;

	void vehicle_info1_cb(Vehicle_info1& canData){
		if(info1.rotation_ != canData.rotation_){
			info1.rotation_ = canData.rotation_;
			tachEnd = info1.rotation_ * 180 / 7000;
		    if(!REFRESHING.isRunning() && tachEnd != tachStart) {
		    	REFRESHING.run();
		    }
		}
		if(info1.speed_ != canData.speed_){
			info1.speed_ = canData.speed_;
			setSpeed(mSpeed2Ptr,info1.speed_);
			speedEnd = info1.speed_ * 179 / 280;
		    if(!REFRESHING.isRunning() && speedEnd != speedStart) {
		    	REFRESHING.run();
		    }
		}
	}
	bool isRegistered = false;
	void condition_cb(V_Condition& canData){
		if(info0.gear != canData.gear || info0.gearGrade != canData.gearGrade || info0.gearRank != canData.gearRank){
			info0.gear = canData.gear; info0.gearGrade = canData.gearGrade; info0.gearRank = canData.gearRank;
			setGear1(mgearPtr,mgearRankPtr,isRegistered);
		}
	}
	void setOilPressure(){
		if(unitStatus.s_pressure){
			mCircleBarOilPtr->setProgress(info3.OilPressure);
			mOilPressurePtr->setText(info3.OilPressure);
		}else{
			mCircleBarOilPtr->setProgress(info3.OilPressure/10);
			char buf[16]={};
			sprintf(buf,"%.1f",info3.OilPressure/10.0);
			mOilPressurePtr->setText(buf);
		}
	}
	void vehicle_info3_cb(Vehicle_info3& canData){
		if(info3.OilPressure != canData.OilPressure){
			info3.OilPressure = canData.OilPressure;
			setOilPressure();
		}
	}
	void setActivityWin(key_operation_e& operation){
		switch(operation){
		case KEY_UP:{
			EASYUICONTEXT->openActivity("mode9Activity");
		}break;
		case KEY_DOWN:
			EASYUICONTEXT->openActivity("mode2Activity");
			break;
		case KEY_LEFT:
	        windowManager.hideCurrentAndShowPrevious();  // 隐藏当前窗口，显示上个窗口
	        if(windowManager.getIndex() == 2){
	        	hideTachBar();
	        }else{
	        	showTachBar();
	        }
			break;
		case KEY_RIGHT:
	        windowManager.hideCurrentAndShowNext();  // 隐藏当前窗口，显示下个窗口
	        if(windowManager.getIndex() == 2){
	        	hideTachBar();
	        }else{
	        	showTachBar();
	        }
			break;
		default:
			return;
		}
	}
	void LongEnterSet(key_operation_e& operation) {
		switch (operation) {
//		case KEY_UP:{
//			EASYUICONTEXT->openActivity("SetupActivity");
//		}break;
		default:
			return;
		}
	}
	void key_value_cb(key_value_t& canData){
		if (canData.pre_status == 1 && canData.status == 0)	//短按
		{
			setActivityWin(canData.operation);
		}else if(canData.pre_status == 1 && canData.status == 2)	//长按
		{
			LongEnterSet(canData.operation);
		}
	}
	void _can_add_cb() {
		_s_can_cb.vehicle_info1_cb = vehicle_info1_cb;
		_s_can_cb.key_value_cb = key_value_cb;
		_s_can_cb.condition_cb = condition_cb;
		_s_can_cb.vehicle_info3_cb = vehicle_info3_cb;
		can::add_cb(&_s_can_cb);
	}

	void _can_remove_cb() {
		can::remove_cb(&_s_can_cb);
	}
	void set_uint(){
		if(unitStatus.s_speed){
			mspeedUnit2Ptr->setText("mph");
		}else{
			mspeedUnit2Ptr->setText("km/h");
		}
		switch(unitStatus.s_pressure){
		case 0:{
			mOilUnitPtr->setText("bar");
			mCircleBarOilPtr->setMax(10);
			mTextView20Ptr->setText("10.2");
		}break;
		case 1:{
			mOilUnitPtr->setText("kpa");
			mCircleBarOilPtr->setMax(1016);
			mTextView20Ptr->setText("1016");
		}break;
		case 2:{
			mOilUnitPtr->setText("psi");
			mCircleBarOilPtr->setMax(147);
			mTextView20Ptr->setText("147");
		}break;
		}
	}
	void Initialization(){
		setSpeed(mSpeed2Ptr,info1.speed_);
		hideSpeedWin();
		setGear(mgearPtr,mgearRankPtr,isRegistered);
		REFRESHING.PointerRpm = mPointerRpmPtr;
		REFRESHING.PointerSpeed = mPointerSpeedPtr;
		tachEnd = info1.rotation_ * 180 / 7000;
		speedEnd = info1.speed_ * 179 / 280;
		REFRESHING.run();
		setOilPressure();
	}
	void set_font(){
		setCtrlFont(mSpeed2Ptr);
		setCtrlFont(mspeedUnit2Ptr);
		setCtrlFont(mPIRULENPtr,PIRULEN);
		setCtrlFont(mgearRankPtr,PIRBOLD);
	}

}

static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {

};

/**
 * 当界面构造时触发
 */
static void onUI_init(){

	set_font();
	globalWindows = {
		mWinSporPtr,
		mWinSpartPtr,
		mWinAnalogPtr,
		mWinDigitalPtr,
	};
    //初始化窗口管理器的窗口列表
    windowManager.setWindows(globalWindows);
    AddCallback(set_uint);
    set_uint();
}

/**
 * 当切换到该界面时触发
 */
static void onUI_intent(const Intent *intentPtr) {
    if (intentPtr != NULL) {
        //TODO
    }
}

/*
 * 当界面显示时触发
 */
static void onUI_show() {
	_can_add_cb();
	Initialization();
}

/*
 * 当界面隐藏时触发
 */
static void onUI_hide() {
	_can_remove_cb();
}

/*
 * 当界面完全退出时触发
 */
static void onUI_quit() {

}

/**
 * 串口数据回调接口
 */
static void onProtocolDataUpdate(const SProtocolData &data) {

}

/**
 * 定时器触发函数
 * 不建议在此函数中写耗时操作，否则将影响UI刷新
 * 参数： id
 *         当前所触发定时器的id，与注册时的id相同
 * 返回值: true
 *             继续运行当前定时器
 *         false
 *             停止运行当前定时器
 */
static bool onUI_Timer(int id){
	switch (id) {

		default:
			break;
	}
    return true;
}

/**
 * 有新的触摸事件时触发
 * 参数：ev
 *         新的触摸事件
 * 返回值：true
 *            表示该触摸事件在此被拦截，系统不再将此触摸事件传递到控件上
 *         false
 *            触摸事件将继续传递到控件上
 */
static bool onmode1ActivityTouchEvent(const MotionEvent &ev) {
    switch (ev.mActionStatus) {
		case MotionEvent::E_ACTION_DOWN://触摸按下
			//LOGD("时刻 = %ld 坐标  x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
			break;
		case MotionEvent::E_ACTION_MOVE://触摸滑动
			break;
		case MotionEvent::E_ACTION_UP:  //触摸抬起
			break;
		default:
			break;
	}
	return false;
}
